package org.wu.framework.authorization.platform.application.impl;

import org.springframework.stereotype.Component;
import org.wu.framework.authorization.domain.LoginUserBO;
import org.wu.framework.authorization.login.UserDetailsService;
import org.wu.framework.authorization.model.AuthorizationUser;
import org.wu.framework.authorization.model.DefaultPermission;
import org.wu.framework.authorization.model.UserDetails;
import org.wu.framework.authorization.platform.application.dto.SysPermissionDTO;
import org.wu.framework.authorization.platform.domain.model.sys.permission.SysPermission;
import org.wu.framework.authorization.platform.domain.model.sys.permission.SysPermissionRepository;
import org.wu.framework.authorization.platform.domain.model.sys.role.SysRole;
import org.wu.framework.authorization.platform.domain.model.sys.role.SysRoleRepository;
import org.wu.framework.authorization.platform.domain.model.sys.role.permission.SysRolePermission;
import org.wu.framework.authorization.platform.domain.model.sys.role.permission.SysRolePermissionRepository;
import org.wu.framework.authorization.platform.domain.model.sys.user.SysUser;
import org.wu.framework.authorization.platform.domain.model.sys.user.SysUserRepository;
import org.wu.framework.authorization.platform.domain.model.sys.user.role.SysUserRole;
import org.wu.framework.authorization.platform.domain.model.sys.user.role.SysUserRoleRepository;
import org.wu.framework.authorization.platform.infrastructure.persistence.SysUserRoleRepositoryImpl;
import org.wu.framework.core.NormalUsedString;
import org.wu.framework.core.utils.ObjectUtils;
import org.wu.framework.lazy.orm.database.lambda.stream.lambda.LazyLambdaStream;
import org.wu.framework.lazy.orm.database.lambda.stream.wrapper.LazyWrappers;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.wu.framework.web.response.Result;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ Description : 当前框架默认实现方法 @ Author : wujiawei @ CreateDate : 2019/12/17 0017 11:46 @ UpdateUser
 * : wujiawei @ UpdateDate : 2019/12/17 0017 11:46 @ UpdateRemark : 修改内容 @ Version : 1.0
 */
@Component
public class PlatformUserDetailsService implements UserDetailsService {


    private final SysUserRepository sysUserRepository;
    private final LazyLambdaStream lazyLambdaStream;
    private final PasswordEncoder passwordEncoder;


    public PlatformUserDetailsService(SysUserRepository sysUserRepository,
                                      LazyLambdaStream lazyLambdaStream,
                                      PasswordEncoder passwordEncoder) {
        this.sysUserRepository = sysUserRepository;
        this.lazyLambdaStream = lazyLambdaStream;
        this.passwordEncoder = passwordEncoder;
    }

    @Override
    public UserDetails loadUserByUsername(String userName) {
        AuthorizationUser authorizationUser = lazyLambdaStream
                .selectOne(
                        LazyWrappers.<LoginUserBO>lambdaWrapper()
                                .eq(LoginUserBO::getUsername, userName), AuthorizationUser.class
                );
        if (ObjectUtils.isEmpty(authorizationUser)) {
            return null;
        }
        // 查询用户权限
        Long id = authorizationUser.getId();
        Result<List<SysPermission>> userPermission = sysUserRepository.findUserPermissionList(id);
        if (userPermission.isSuccess()) {
            List<SysPermission> sysPermissionList = userPermission.getData();
            List<DefaultPermission> defaultPermissionList =
                    sysPermissionList
                            .stream().map(sysPermission -> {
                                String url = sysPermission.getUrl();
                                String method = sysPermission.getMethod();
                                DefaultPermission defaultPermission = new DefaultPermission();
                                defaultPermission.setUrl(url);
                                defaultPermission.setMethod(method);
                                return defaultPermission;
                            }).collect(Collectors.toList());
            authorizationUser.setPermissionList(defaultPermissionList);
        }
        return authorizationUser;
    }

    /**
     * 创建用户
     *
     * @param loginUserBO@return
     * @author Jiawei Wu
     * @date 2021/1/6 8:37 下午
     **/
    @Override
    public void createUser(LoginUserBO loginUserBO) {
        String md5Password = passwordEncoder.encode(loginUserBO.getPassword());
        loginUserBO.setPassword(md5Password);
        lazyLambdaStream.upsert(loginUserBO);
    }

    @Override
    public UserDetails loadUserById(String userId) {
        AuthorizationUser authorizationUser = lazyLambdaStream
                .of(LoginUserBO.class)
                .selectOne(
                        LazyWrappers.<LoginUserBO>lambdaWrapper()
                                .eq(LoginUserBO::getId, userId), AuthorizationUser.class
                );
        // 查询用户权限
        Long id = authorizationUser.getId();
        Result<List<SysPermission>> userPermission = sysUserRepository.findUserPermissionList(id);
        if (userPermission.isSuccess()) {
            List<SysPermission> sysPermissionList = userPermission.getData();
            List<DefaultPermission> defaultPermissionList =
                    sysPermissionList
                            .stream().map(sysPermission -> {
                                String url = sysPermission.getUrl();
                                String method = sysPermission.getMethod();
                                DefaultPermission defaultPermission = new DefaultPermission();
                                defaultPermission.setUrl(url);
                                defaultPermission.setMethod(method);
                                return defaultPermission;
                            }).collect(Collectors.toList());
            authorizationUser.setPermissionList(defaultPermissionList);
        }
        return authorizationUser;
    }
}
